package gsw.demo;

import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

import gsw.tool.rxjava.Observable;
import gsw.tool.rxjava.ObservableOnSubscribe;
import gsw.tool.rxjava.Observer;
import gsw.tool.rxjava.R;
import gsw.tool.rxjava.operator.Function;

public class MainActivity extends Activity {

    ImageView imgeView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        imgeView = findViewById(R.id.imageView);
    }

    public void post(View view) {
        Observable.create(new ObservableOnSubscribe<Integer>() {//创建被观察者
            @Override
            public void subscribe(Observer<? super Integer> observer) {//处理消息
                observer.onNext(R.mipmap.icon);
            }
        }).map(new Function<Integer, Drawable>() {//转换发送的数据
            @Override
            public Drawable apply(Integer id) {
                return getResources().getDrawable(id);
            }
        }).subscribe(new Observer<Drawable>() {//订阅观察者
            @Override
            public void onNext(Drawable src) {//观察者处理消息

                imgeView.setImageDrawable(src);
            }

            @Override
            public void onError(Throwable e) {
            }

            @Override
            public void onComplete() {
            }
        });
    }
}
